define(["cmn","bde","lib/render","lib/diqye-pager"],function(cmn,bde){
	var ejs=cmn.ejs("19911204/2/blogList"),eid=777,_eid=777;
	ejs.set("main","main");
	ejs.set("list","list");
	ejs.set("modify","modify");
	return {
		init:function(){
			var _=this;
			_.renderMain();
		},
		renderMain:function(){
			var _=this;
			_.el.html(ejs.main,function(){
				_.buindEvent(_.el.find("#blog_list1020"));
				_.queryList(_.el.find("#blog_list1020"));
			});
		},
		buindEvent:function(div){
			var _=this;
			div.render();
			div.find("#querytitle").on("click",function(){
				div.find("#querywhere").toggleClass("hidden");
			});

			// div.find("#me1020").on("click",function(){
			// 	div.find("#author1020").val(cmn.loginData.qq);
			// });

			// div.find("#all1020").on("click",function(){
			// 	div.find("#author1020").val('');
			// });
			div.find("#reset1020").on("click",function(){
				div.reset();
			});
			div.find("#query1020").on("click",function(){
				if(div.validate()){
					_.queryList(div);
					div.find("#querytitle").trigger("click");
				}	
			});
		},
		queryList:function(div){
			var _=this,model,params=div.formToObject();
			params.currentIndex=0;
			params.pageSize=10;
			model={
				action:"Blog_queryLi",
				params:params
			};
			function pagefn(count){
				
				div.find("#pagination").pagination({
					ejs:cmn.cmn_ejs.pagination,
					param:{
						count:count
					},
					callback:function(i){
						model.params.currentIndex=(i-1)*10;
						_.renderList(div,model);
					}
				});
			}
			_.renderList(div,model,pagefn);

		},
		renderList:function(div,model,pagefn){
			var _=this,checkboxs,checkeds,csize,mdata;
			cmn.lbox.show();
			cmn.post(model).then(function(data){
				cmn.lbox.hide();
				renderHtml(data);
				mdata=data;
				if(pagefn)pagefn(data.count);
			});

			function renderHtml(data){
				div.find("#tabledata1020").html(ejs.list,data,rend);
			}

			function rend(){
				checkboxs=div.find("input[index]");
				checkboxs.on("click",selsize);
				div.find("#selectAll1021").off("click").on("click",function(){
					if(div.find(this).attr("cmd")=="Y"){
						checkboxs.attr("checked",true);
					}else{
						checkboxs.attr("checked",false);
					}
					selsize();
				});
				div.find("#del1021").off("click").on("click",function(){
					if(div.find(this).parent("li").is(".disabled"))return;
					var ids=[];
					checkeds.each(function(){
						var i=parseInt(div.find(this).attr("index"));
						ids.push(mdata.list[i].id);
					});
					cmn.lbox.show();
					cmn.post({
						action:"Blog_deleteTgLi",
						params:{
							ids:ids
						}
					}).then(function(){
						_.renderMain();
					});
				});
				//点击修改
				div.find("[cmd=modify]").on("click",function(){
					var index=parseInt(div.find(this).attr("index"));
					_.renderModify(mdata.list[index]);
				});
				selsize();
			}

			function selsize(){
				checkeds=div.find("input[index]:checked");
				csize=checkeds.size();
				div.find("#selsize1021").text("已勾选"+csize);
				if(csize==checkboxs.size()){
					div.find("#selectAll1021").text("反选").attr("cmd","N");
					
				}else{
					div.find("#selectAll1021").text("全选").attr("cmd","Y");
					
				}

				if(csize>0)div.find("#del1021").parent("li").removeClass("disabled");
				else div.find("#del1021").parent("li").addClass("disabled");
			}	
		},
		renderModify:function(blog){
			var _=this,mdata;
			cmn.lbox.show();
			cmn.post({
				action:"Blog_queryOne",
				params:{
					id:blog.id
				}
			}).then(function(data){
				eid=_eid++;
				eid+="";
				data.eid=eid;
				mdata=data;
				cmn.lbox.hide();
				_.el.html(ejs.modify,data,end);
			});
			function end(){
				bde.getEditor(eid,{
    			 	zIndex : 6,
    			 	autoClearinitialContent:true,
    			 	autoHeightEnabled:false,
    			 	autoFloatEnabled:false,
    			 	autoClearinitialContent:false,
    			 	toolbar:[
				            'source | undo redo | bold italic underline strikethrough | superscript subscript | forecolor backcolor | removeformat |',
				            'insertorderedlist insertunorderedlist |  cleardoc paragraph | fontfamily fontsize' ,
				            '| justifyleft justifycenter justifyright justifyjustify |',
				            'link unlink | emotion | map',
				            '| horizontal print preview ', 'drafts', 'formula','warp code label'
				    ]
    			});
    			_.el.find("#return1021").on("click",function(){
    				cmn.confirm("保存确认","确定要返回？",function(){
    					_.renderMain();
    				});
    			});
    			_.el.find("#publish1021").on("click",function(){
    				if(_.el.validate()){
    					if(!bde.getEditor(eid).hasContents()){
    						cmn.alert("博文内容不能为空");
    					}else{
    						cmn.lbox.show();
    						cmn.post({
    							action:"Blog_updateLi",
    							params:{
    								id:mdata.id,
    								title:_.el.find("#title1021").val(),
    								tag:_.el.find("#tag1021").val(),
    								content:bde.getEditor(eid).getContent()
    							}
    						}).then(function(){
    							cmn.lbox.hide();
    							_.renderMain();
    						});
    					}
    				}
    			});
			}
		}
///////////////////////////////end////////////////////////////////


	}
});